import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from '/@/model/userModel';
import Qs from 'qs'//引入q

import { ErrorMessageMode } from '/#/axios';

enum Api {
  Login = '/authorize/oauth/token',
  Logout = '/authorize/token/logout',
  GetUserInfo = '/upms/user/info',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  const grant_type = 'password';
  const scope = 'server';
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login + '?' + Qs.stringify({ grant_type, scope, ...params }),
      headers: {
        Authorization: 'Basic anVseTpqdWx5',
      },
    },
    {
      errorMessageMode: mode,
      isTransformResponse:false
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'modal' });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.delete({ url: Api.Logout });
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}
